<?php
class LyContext {
	public $params = array();
	protected $options = array();
	function setOptions($options){
		$this->options = array_merge($this->options,$options);
	}
	function __construct(){
		$this->fliterInput();
		$this->isAjax();
	}
	protected function isAjax(){
		if(isset($_SERVER['HTTP_X_REQUESTED_WITH']) || isset($this->params['ajax'])){
			$this->params['ajax'] = true;
		}else{
			$this->params['ajax'] = false;
		}
	}
	function __get($name){
		return $this->getParams($name);
	}
	function __set($name,$value){
		$array[strtolower($name)] = $value;
		$this->addParams($array);
	}
	/**
	 * @todo 过滤/转义 GET,POST,COOKIE中的不安全字符
	 * @return void
	 */
	function fliterInput(){

	}
	/**
	 * 添加参数
	 * @param $params
	 * @return unknown_type
	 */
	function addParams($params){
		if(is_array($params) && !empty($params)){
			foreach ($params as $key => $value){
				$this->params[strtolower($key)] = strtolower($value);
			}
		}
	}
	/**
	 * 获得参数
	 * @param unknown_type $name
	 * @param unknown_type $default
	 * @return unknown_type
	 */
	function getParams($name,$default = null){
		$name = strtolower($name);
		if(isset($this->params[$name])){
			return $this->params[$name];
		}
		if(isset($_GET[$name])){
			return $_GET[$name];
		}
		if(isset($_POST[$name])){
			return $_POST[$name];
		}
		if(isset($_COOKIE[$name])){
			return $_COOKIE[$name];
		}
		return $default;
	}
	/**
	 * 获得GET数据
	 * @param unknown_type $name
	 * @return unknown_type
	 */
	function getGet($name = null){
		if(null === $name){
			return $_GET;
		}
		$name = strtolower(trim($name));
		if(isset($_GET[$name])){
			return $_GET[$name];
		}
		return $default;
	}
	/**
	 * 获得POST数据
	 * @param $name
	 * @return unknown_type
	 */
	function getPost($name = null){
		if(null === $name){
			return $_POST;
		}
		$name = strtolower(trim($name));
		if(isset($_POST[$name])){
			return $_POST[$name];
		}
		return $default;
	}
	/**
	 * 获得COOKIE数据
	 * @param $name
	 * @return unknown_type
	 */
	function getCookie($name = null){
		if(null === $name){
			return $_COOKIE;
		}
		$name = strtolower(trim($name));
		if(isset($_COOKIE[$name])){
			return $_COOKIE[$name];
		}
		return $default;
	}
	function getUser(){

	}
}
